package com.bjbn.tasks;

import com.bjbn.service.impl.RabbitmqMonitorServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * RabbitMQ监控定时任务
 * 
 * @author bjbn
 */
@Slf4j
@Component
public class RabbitmqMonitorTask {

    @Autowired
    private RabbitmqMonitorServiceImpl monitorService;
    
    @Value("${monitor.check-interval-seconds:30}")
    private int checkIntervalSeconds;

    /**
     * 定期清理过期数据
     * 每5分钟执行一次
     */
    @Scheduled(fixedRate = 300000)
    public void cleanupExpiredData() {
        try {
            log.debug("开始清理过期数据...");
            monitorService.cleanupExpiredData();
            log.debug("清理过期数据完成，时间: {}", LocalDateTime.now());
        } catch (Exception e) {
            log.error("清理过期数据时发生错误", e);
        }
    }

    /**
     * 定期检查队列状态
     * 每30秒执行一次
     */
    @Scheduled(fixedRateString = "${monitor.check-interval-seconds:30}000")
    public void checkQueueStatus() {
        try {
            // 获取所有队列状态，内部会自动更新状态
            monitorService.getMonitorStatus();
            log.debug("定期检查队列状态完成，时间: {}", LocalDateTime.now());
        } catch (Exception e) {
            log.error("检查队列状态时发生错误", e);
        }
    }
}